package com.tongji.housediy.service
{
	import com.tongji.housediy.events.CantavilEvent.GetAllCityEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilDetailEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilNameEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilResultEvent;
	import com.tongji.housediy.model.CantavilModel;
	import com.tongji.housediy.vo.Model;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author kevin
	 * 2011/5/7
	 */ 
	public class CantavilService extends Actor
	{
	
		public var cantavilRemoteObject:RemoteObject;
		
		private var cantavilModel:CantavilModel;
		
		public function CantavilService()
		{
			super();
			
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.118:81/Gateway.aspx")];
			var amfChannel:Array = [new AMFChannel(null, "http://design.rryjw.com/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediy/Gateway.aspx")];

			//var amfChannel:Array = [new AMFChannel(null, "http://ssegrc.gotoip55.com/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");

			cantavilRemoteObject = new RemoteObject("fluorine");
			cantavilRemoteObject.source = "HouseDiyService.HouseDiyAction";
			cantavilRemoteObject.showBusyCursor = true;
			cantavilRemoteObject.channelSet = channelSet;
			
			cantavilRemoteObject.getOperation("searchCantavilByWord").addEventListener(ResultEvent.RESULT, getCantavilNameResult);
			cantavilRemoteObject.getOperation("searchCantavilByWord").addEventListener(FaultEvent.FAULT, faultHandler);
			
			cantavilRemoteObject.getOperation("searchByCantavilName").addEventListener(ResultEvent.RESULT, getCantavilByCantavilNameResult);
			cantavilRemoteObject.getOperation("searchByCantavilName").addEventListener(FaultEvent.FAULT, faultHandler);
			
			cantavilRemoteObject.getOperation("getAllCityID").addEventListener(ResultEvent.RESULT, getAllCitysResult);
			cantavilRemoteObject.getOperation("getAllCityID").addEventListener(FaultEvent.FAULT, faultHandler);
			
			cantavilRemoteObject.getOperation("getCantavils").addEventListener(ResultEvent.RESULT, getCantavilsResult);
			cantavilRemoteObject.getOperation("getCantavils").addEventListener(FaultEvent.FAULT, faultHandler);
		
			cantavilRemoteObject.getOperation("getCantavilDetailByTypeID").addEventListener(ResultEvent.RESULT, getCantavilDetailResult);
			cantavilRemoteObject.getOperation("getCantavilDetailByTypeID").addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function getCantavilResult(searchName:String):void
		{
			cantavilRemoteObject.searchByCantavilName(searchName);
		}
		
		public function getCantavilName(cantavilName:String):void
		{
			cantavilRemoteObject.searchCantavilByWord(cantavilName);
		}
		
		public function getCity():void
		{
			cantavilRemoteObject.getAllCityID();
		}
		
		public function getCantavilDetail(cantavilTypeID:int):void
		{
			cantavilRemoteObject.getCantavilDetailByTypeID(cantavilTypeID);
		}
		
		public function getAllCantavils():void
		{
			cantavilRemoteObject.getCantavils();
		}
		
		public function getCantavilByCantavilNameResult(event:ResultEvent):void
		{
			dispatch(new GetCantavilResultEvent(GetCantavilResultEvent.UPDATE_CANTAVIL_BY_CANTAVIL_NAME, null, event.result as ArrayCollection));
		}
		
		public function getCantavilNameResult(event:ResultEvent):void
		{
			dispatch(new GetCantavilNameEvent(GetCantavilNameEvent.UPDATE_CANTAVIL_MODEL, null, null, event.result as ArrayCollection));
		}
		
		private function getCantavilsResult(event:ResultEvent):void
		{
		    dispatch(new GetCantavilEvent(GetCantavilEvent.UPDATE_CANTAVIL, event.result as ArrayCollection));
		}
		
		private function getAllCitysResult(event:ResultEvent):void
		{
			dispatch(new GetAllCityEvent(GetAllCityEvent.UPDATE_CITY_LIST, event.result as ArrayCollection));
		}
		
		private function getCantavilDetailResult(event:ResultEvent):void
		{
			dispatch(new GetCantavilDetailEvent(GetCantavilDetailEvent.UPDATE_CANTAVIL_DETAIL, 7, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}